using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Configuration;

using HermesPortalUI;
using HermesPortal;
using HermesPortal.Core;  
/// <summary>
/// Summary description for PageBase
/// </summary>
public abstract  class PageBase : System.Web.UI.Page
{
    int _sagaID = 0;
    private Creator _creator = null;
    public int SagaID
    {
        get
        {
            if (_sagaID == 0)
            {
                if(Request.QueryString["SagaID"] != null  )
                {
                    if (Int32.TryParse(Request.QueryString["SagaID"].ToString() ,out _sagaID))
                    {
                        Session["SagaID"] = _sagaID;
                        return _sagaID;
                    }
                }
                if (Session["SagaID"] != null)
                {
                    _sagaID = (int)Session["SagaID"];
                }
                else { Response.Redirect(Request.ApplicationPath + @"\Default.apx"); } 

            }
                 
                return _sagaID;
            }
        set {  
                _sagaID = value;
                Session["SagaID"] = _sagaID;
            }
    }

    public Creator Creator
    {
        get {
            if (_creator == null && User.Identity.IsAuthenticated   )
            {
                if (Session["Creator"] != null)
                {
                    _creator =(Creator) Session["Creator"];
                }
                else
                {
                    _creator = UserServices.GetCreatorByUserName(User.Identity.Name );
                    Session["Creator"] = _creator; 
                }
            }
            else if (_creator != null && User.Identity.IsAuthenticated)
            {
                if (_creator.CreatorID == 0)
                {
                    _creator = UserServices.GetCreatorByUserName(User.Identity.Name);
                    Session["Creator"] = _creator;
                }

            }
            else { return GetDefaultCreator(); }

            return _creator;
            }
           
    }
    public readonly static ConfigSection HermesPortalConfig =
        (ConfigSection) WebConfigurationManager.GetSection("hermesPortalConfig");

    protected override void OnLoad(EventArgs e)
    {
        //UIHelpers.SetFormFieldHighlightScript(this, "inputHighlight");
        base.OnLoad(e);
    }

    protected override void OnError(EventArgs e)
    {
        //log error in sql, email error, make color-by-number error book
       
        
        base.OnError(e);
    }

    protected bool IsInSagaRole(int creatorId, int sagaId, SagaRoles role)
    {
        if (role == SagaRoles.Browser)
            return true;
       return PortalSecurity.IsInRole(creatorId,sagaId,role);    
        
    }

    protected bool IsInSagaRole( SagaRoles role)
    {
        if (role == SagaRoles.Browser)
            return true;

        return (role == UserServices.GetCreatorRoleForSaga(Creator.Id , SagaID));

    }
    // creats a dummy creator obj for unauthenticated users
    private Creator GetDefaultCreator()
    {
        Creator creator = new Creator();
        creator.CanAdd = false;
        creator.IsBrowser = true;
        creator.SagaRoleID = SagaRoles.Browser;
        creator.UserName = "Browser";
        creator.Id = 0;
        creator.SagaRoleName = "Browser";

        return creator;  
    }

}
